import { Toast } from "vant";
import { addCart, delCart, getCart, updateCart } from "../../request/api";

const state = {
    data: {
        uid: "",
        goodsid: "",
        num: 1,
    },
    goods: [],
    good:[]

}

// 2,声明mutations
const mutations = {
    // 专门来修改goods数据
    changegoodsid(state, goodsid) {
        state.data.goodsid = goodsid
    },
    // changenum(state, num) {
    //     state.data.num = num
    // },
    changeuid(state, uid) {
        state.data.uid = uid
    },
    changegoods(state, goods) {
        state.goods = goods
    },
    changegood(state,good){
        state.good=good
    }

}

// 3.声明actions
const actions = {
    // denumActions(context){//减少num
    //     if(context.state.data.num<=0){
    //         context.commit('changenum',0)
    //     }else{
    //         context.commit('changenum',context.state.data.num-1)
    //     } 
    // },
    // addnumActions(context){//增加num
    //     context.commit('changenum',context.state.data.num+1)
    // },
    uidActions(context) {//获取uid
        let uid = JSON.parse(sessionStorage.getItem('user')).uid
        // console.log(uid);
        context.commit('changeuid', uid)
    },
    goodsidActions(context, goodsid) {//获取goodsid
        context.commit('changegoodsid', goodsid)
    },
    addcartActions(context, obj) {//增加购物车
        addCart(obj).then(res => {
            // console.log(888);
            if (res.code == 200) {
                Toast(res.msg)
                console.log(res);
            }
        })
    },
    // clearnum(context){//清除num
    //     context.commit('changenum',0)
    // },
    cartActions(context, uid) {//获取购物车列表
        // console.log(123);
        getCart({ uid }).then(res => {
            // console.log(uid);
            // console.log(res);
            if (res.code == 200) {
                // console.log(res);
                // console.log(123);
                // console.log('购物车');
                context.commit('changegoods', res.list)
                // console.log(res.list);
            }
        })
    },
    changeCartActions(context, obj) {
            updateCart(obj).then(res => {
                if (res.code == 200) {
                    // console.log(res);
                    // context.commit()
                    let uid = JSON.parse(sessionStorage.getItem('user')).uid
                    context.dispatch('cartActions',uid)
                }
            })
        

    },
    delActions(context,id){
        delCart({id}).then(res=>{
            if(res.code==200){
                Toast(res.msg)
                // console.log(res);
                let uid = JSON.parse(sessionStorage.getItem('user')).uid
                context.dispatch('cartActions',uid)
            }
        })
    },
    changegoodActions(context,obj){
        context.commit('changegood',obj)
    }

}
// 4.声明getters
const getters = {
    data(state) {
        return state.data
    },
    goods(state) {
        return state.goods
    },
    good(state) {
        return state.good
    }
}

export default {
    state,
    mutations,
    actions, getters,
    namespaced: true,//使用命名
}